package com.puge.eduservice.controller;


import com.puge.commonutils.R;
import com.puge.eduservice.client.VodClient;
import com.puge.eduservice.entity.EduVideo;
import com.puge.eduservice.service.EduVideoService;
import com.puge.exceptionhandle.PuGeException;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-06-14
 */

@Api(description = "课程小节管理")//设置接口文档名称
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class EduVideoController {

    @Autowired
    private EduVideoService videoService;

    //注入VodClient
    @Autowired
    private VodClient vodClient;

    //添加小节
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        videoService.save(eduVideo);
        return R.ok();
    }

    //删除小节,删除小节的时候，同时把阿里云里面的视频删除
    @DeleteMapping("{id}")
    public R deleteVideo(@PathVariable String id){
        System.out.println("id"+id);

        //根据小节id获取视频id：删除小节的时候，同时把阿里云里面的视频删除
        EduVideo eduVideo = videoService.getById(id);
        String videoSourceId = eduVideo.getVideoSourceId();

        //判断小节里面是否有视频id
        if (!StringUtils.isEmpty(videoSourceId)){
            System.out.println("videoSourceId:"+videoSourceId);

            //1.根据视频id，远程调用实现删除视频
            R result = vodClient.removeAlyVideo(videoSourceId);
            if (result.getCode() ==20001){
                throw new PuGeException(2001,"删除视频失败，熔断器正在启动。。。。。。。。");
            }
        }
        //2，删除小节
        videoService.removeById(id);
        return R.ok();
    }

    //修改小节 TODO
    @PostMapping("updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo){
        videoService.updateById(eduVideo);
        return R.ok();
    }


}

